package com.fs.chatx.core.handler;

import com.fs.chatx.core.api.R;
import com.fs.chatx.core.exception.ApiException;
import com.fs.chatx.module.common.exception.MinioOperationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常统一处理
 *
 * @author tarzan liu
 * @date 2025年3月11日
 * @since JDK17
 */

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler
{

    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public R<String> handleException(NullPointerException e) {
        log.error("空指针异常: {}", e.getMessage(), e);
        return R.fail(500, e.getMessage());
    }

    @ExceptionHandler(ApiException.class)
    @ResponseBody
    public R<String>  handleException(ApiException e) {
        return R.fail(400, e.getMessage());
    }


    @ExceptionHandler(MinioOperationException.class)
    public R<String> handleMinioException(MinioOperationException e)
    {
        return R.fail(500,"MinIO 操作失败: " + e.getMessage());
    }

}
